/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package control

import dao.SamplesDAO
import entity.Sample
import java.sql.Connection
import entity.ServiceRequest
/**
 *
 * @author Tato
 */
class ControlSample {
    
    SamplesDAO dao = new SamplesDAO()
    
    def static printBarcodes(samples) {
        samples.each {boundary.barcode.GenerateBarcode.generarCodigo(it.toString());
            boundary.barcode.ImprimirCode.imprimir(it.toString())}
    }
    
    def static printBarcode(sample) {
        boundary.barcode.GenerateBarcode.generarCodigo(sample.toString())
        boundary.barcode.ImprimirCode.imprimir(sample.toString())
    }
    
    def static Boolean searchCode(String code) {
        Collection serviceRequests = Application.SERVICE_REQUEST_MANAGER.getCollection()
        Boolean found = false
        
        for (serviceRequest in serviceRequests) {
            def samples = serviceRequest.getSamples()
            for (sample in samples) {
                if (sample.toString().equals(code)) {
                    Application.displayEntity(sample)
                    found = true
                    break
                }
            }
        }
        found
    }
     
    def addSample(Sample sample){
       
        key = dao.create(sample,DBConnection.establishConection())
        if(key > 0){
            sample.id= key
            "Agregado"
        }
          
    }
    
    def deleteSample(Sample sample){
        
        delete = dao.remove(sample,DBConnection.establishConection())
        
        if(delete == true){
            "Borrado exitoso"
        }
        else{
            "Borrado falló"
        }     
    }
    
    def updateSample(Sample sample){
        if(dao.update(sample,DBConnection.establishConection()) > 0){
               "Información actualizada"
            }
            
            else {
                "Información no actualizada"
            }
        
    }
    
     def findSample(Integer id){
         if(dao.find(id,DBConnection.establishConection())!= null  ){
               "Información actualizada"
            }
            
            else {
                "Información no actualizada"
            }
        
    }
	
}

